//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package issue_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type DependDbItem struct {
	DependId      string `bson:"_id"`
	ProjectId     string `bson:"projectId"`
	IssueId       string `bson:"issueId"`
	DependIssueId string `bson:"dependIssueId"`
}

type _DependDao struct {
	mc *mongo.Collection
}

func newDependDao(db *mongo.Database) (*_DependDao, error) {
	mc := db.Collection(db_ns.DB_NS_ISSUE + "depend")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"projectId", 1}, {"issueId", 1}},
		},
		{
			Keys: bson.D{{"projectId", 1}, {"dependIssueId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_DependDao{mc: mc}, nil
}

func (db *_DependDao) genDependId(issueId, dependIssueId string) string {
	return fmt.Sprintf("%s:%s", issueId, dependIssueId)
}

func (db *_DependDao) Insert(ctx context.Context, dbItem *DependDbItem) error {
	dbItem.DependId = db.genDependId(dbItem.IssueId, dbItem.DependIssueId)
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_DependDao) Remove(ctx context.Context, projectId, issueId, dependIssueId string) error {
	dependId := db.genDependId(issueId, dependIssueId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": dependId, "projectId": projectId})
	return err
}

func (db *_DependDao) RemoveByIssue(ctx context.Context, projectId, issueId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "issueId": issueId})
	return err
}

func (db *_DependDao) RemoveByDependIssue(ctx context.Context, projectId, dependIssueId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "dependIssueId": dependIssueId})
	return err
}

func (db *_DependDao) DistinctByIssue(ctx context.Context, projectId, issueId string) ([]string, error) {
	itemList, err := db.mc.Distinct(ctx, "dependIssueId", bson.M{"projectId": projectId, "issueId": issueId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, item := range itemList {
		retList = append(retList, item.(string))
	}
	return retList, nil
}

func (db *_DependDao) DistinctByDependIssue(ctx context.Context, projectId, dependIssueId string) ([]string, error) {
	itemList, err := db.mc.Distinct(ctx, "issueId", bson.M{"projectId": projectId, "dependIssueId": dependIssueId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, item := range itemList {
		retList = append(retList, item.(string))
	}
	return retList, nil
}

func (db *_DependDao) DistinctAllDependIssueId(ctx context.Context, projectId string) ([]string, error) {
	itemList, err := db.mc.Distinct(ctx, "dependIssueId", bson.M{"projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, item := range itemList {
		retList = append(retList, item.(string))
	}
	return retList, nil
}
